package com.study.blogsearchproducer.controller;

import com.study.blogsearchpojo.entity.Blog;
import com.study.blogsearchpojo.entity.BlogBatchDto;
import com.study.blogsearchpojo.entity.BlogOneDto;
import com.study.blogsearchproducer.service.SearchService;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author yana
 * @date 2021-12-14
 */
@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 判断索引库是否已经存在
     */
    @PostMapping("/exists")
    @ResponseBody
    boolean exists(@RequestParam(value = "indexName")String indexName){
        return searchService.exists(indexName);
    }

    /**
     * 删除索引库
     */
    @PostMapping("/dropIndex")
    @ResponseBody
    boolean dropIndex(@RequestParam(value = "indexName")String indexName){
        return searchService.dropIndex(indexName);
    }

    /**
     * 创建博客索引库（标题、内容、作者、博客id） （标题、内容、作者可以分词检索）
     */
    @PostMapping("/createBlogIndex")
    @ResponseBody
    boolean createBlogIndex(@RequestParam(value = "indexName")String indexName){
        return searchService.createBlogIndex(indexName);
    }




    /**
     * 录入一条博客
     */
    @PostMapping("/saveOne")
    @ResponseBody
    void saveOne(@RequestBody BlogOneDto blogOneDto){
        searchService.saveOne(blogOneDto.getIndexName(),blogOneDto.getBlog());
    }
    /**
     * 批量录入博客
     */
    @PostMapping("/addBlogsInBatches")
    @ResponseBody
    void addBlogsInBatches(@RequestBody BlogBatchDto blogBatchDto){
        searchService.addBlogsInBatches(blogBatchDto.getIndexName(),blogBatchDto.getBlogList());
    }
    /**
     * 判断一条是否存在
     */
    @PostMapping("/existsOne")
    @ResponseBody
    boolean existsOne(@RequestParam(value = "indexName")String indexName, @RequestParam(value = "id")String id){
        return searchService.existsOne(indexName,id);
    }
    /**
     * 删除一条
     */
    @PostMapping("/deleteOne")
    @ResponseBody
    boolean deleteOne(@RequestParam(value = "indexName")String indexName,@RequestParam(value = "id") String id){
        DeleteResponse deleteResponse = searchService.deleteOne(indexName, id);
        if (deleteResponse.getResult() == DocWriteResponse.Result.DELETED) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 更新一条博客
     */
    @PostMapping("/updateOne")
    @ResponseBody
    boolean updateOne(@RequestBody BlogOneDto blogOneDto){
        UpdateResponse updateResponse = searchService.updateOne(blogOneDto.getIndexName(), blogOneDto.getBlog());
        if (Objects.nonNull(updateResponse)&& updateResponse.getResult() == DocWriteResponse.Result.CREATED||updateResponse.getResult()==DocWriteResponse.Result.NOOP) {
            //更新的博客不存在，-》创建create            更新值和原值相同 noop
            return true;
        } else {
            if (updateResponse.getResult() != DocWriteResponse.Result.UPDATED) {
               return false;
            }else {
                //更新update
                return true;
            }
        }
    }
    /**
     * 根据关键词  将标题、作者、内容与搜索词相关的博客检索出来
     */
    @PostMapping("/search")
    @ResponseBody
    List<Blog> search(@RequestParam(value = "indexName") String indexName,@RequestParam(value ="keyword" ) String keyword,@RequestParam(value="pageNo")int pageNo){
        List<Blog> blogList = searchService.search( indexName, keyword,pageNo);
        return blogList;
    }
//
//    /**
//     * 根据关键词  将标题、作者、内容与搜索词相关的博客检索出来
//     */
//    @PostMapping("/suggest")
//    @ResponseBody
//    List<String> suggest(String indexName, String keyword){
//        return null;
//    }
}
